﻿/**
* jQuery EasyUI 1.4.3
* Copyright (c) 2009-2015 www.jeasyui.com. All rights reserved.
*
* Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt
* To use it on other terms please contact us at info@jeasyui.com
* http://www.jeasyui.com/license_commercial.php
*
* jQuery EasyUI cachePool 扩展
* jeasyui.extensions.cachePool.js
* 开发 落阳
* 最近更新：2017-03-18
*
* 依赖项：
*   1、jquery.jdirk.js
*
* Copyright (c) 2017 Lixilin personal All rights reserved.
*/
(function ($) {

    $.util.namespace("$.easyui.extensions.cachePool");

    var pools = $.easyui.extensions.cachePool;

    pools.caches = [];

    var cacheItemOptions = {
        // 获取数据的远程地址
        url: "",

        // 访问远程地址时使用的参数对象
        queryParams: null,

        // 远程地址返回的数据在经过loadFilter处理后的数据对象
        data: null,

        // 首次访问该url并成功获取数据的easyui组件名称
        createComponent: "",

        // 首次访问该url并成功获取数据的时间
        createTime: null,

        // 上次使用缓存data的easyui组件名称
        lastComponent: null,

        // 上次使用缓存data的时间
        lastUseTime: null,

        // 距lastUseTime多久后丢弃缓存data，单位为秒
        // 如果lastUseTime不存在，则根据createTime计算
        dropDataInterval: undefined,

        // 是否被重载过
        override: false
    };

    var checkCacheValid = function () {
        var len = pools.caches.length;
        for (var index = 0; index < len; index++) {
            var item = pools.caches[index];
            if (item.dropDataInterval == undefined || !$.util.isNumber(item.dropDataInterval)) {
                continue;
            }
            var lastTime = item.lastUseTime ? item.lastUseTime : item.createTime;
            if ($.date.addSeconds(lastTime, item.dropDataInterval) < new Date()) {
                pools.caches.splice(index, 1);
                index--;
                len--;
            }
        }
    };

    var getPool = function () {
        checkCacheValid();
        return pools;
    };

    var getCache = function (params) {
        checkCacheValid();
        if (!$.util.isString(params) && !$.util.isObject(params)) {
            return null;
        }
        if (pools.caches.length == 0) {
            return null;
        }
        if (!params) {
            return pools.caches[0];
        }
        if (arguments.length == 1 && $.util.isString(params)) {
            //按url查询
            return $.array.first(pools.caches, function (item) { return item.url == params });
        }
        return $.array.first(pools.caches, params);
    };

    var addCache = function (url, queryParams, data, component, dropDataInterval) {
        var cache = $.extend({}, cacheItemOptions, {
            url: url,
            queryParams: queryParams,
            data: data,
            createComponent: component,
            createTime: new Date(),
            dropDataInterval: dropDataInterval
        });

        var exist = $.array.first(pools.caches, function (item) { return item.url == cache.url; });
        if (exist) {
            $.extend(exist, cache, {
                override: true
            });
        } else {
            pools.caches.push(cache);
        }
    }

    var methods = {

        getPool: getPool,

        getCaches: getPool,

        getCache: function (params) {
            return getCache(params);
        },

        addCache: function (url, queryParams, data, component, dropDataInterval) {
            addCache(url, queryParams, data, component, dropDataInterval);
        },

        clearCaches: function () {
            pools.caches = [];
        }
    };

    $.easyui.cachePool = $.extend({}, methods);

})(jQuery);